﻿package com.kingtone.rongji.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.Address;
import com.kingtone.rongji.tool.RongJiUtils;

public class StoreAdapter extends BaseAdapter
{
	private ArrayList<Address> addList = null;
	private LayoutInflater inflater;
	private double lat, lng;
	private Context mContext = null;
	private Address defaultAdd = null;

	public StoreAdapter(Context context, ArrayList<Address> addList, double lat, double lng, Address defaultAdd)
	{
		this.inflater=LayoutInflater.from(context);
		this.addList = addList;
		this.lat = lat;
		this.lng = lng;
		this.mContext = context;
		this.defaultAdd = defaultAdd;
	}
	@Override
	public int getCount()
	{
		return addList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return addList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View view;
		 ViewHolder holder = null;
		//如果已加载过，使用缓存技术不再重新加载
		if(convertView!=null){
			view=convertView;
			holder=(ViewHolder) view.getTag();
		}else{
			view=inflater.inflate(R.layout.store_item, null);
			holder = new ViewHolder();
			holder.storeNamePhone = (TextView) view.findViewById(R.id.store_name_phone);
			holder.storeArea =(TextView) view.findViewById(R.id.store_area);
			holder.storeDetail =(TextView) view.findViewById(R.id.store_detail);
			holder.storeUsed =(TextView) view.findViewById(R.id.store_used);
			holder.storeDistance = (TextView)view.findViewById(R.id.store_distance);
		}
		
		/*holder.storeSelect.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				
			}
		});*/
		
		view.setTag(holder);
		Address address = addList.get(position);
		if(address.IsDefault.equals("true"))
		{
			holder.storeUsed.setVisibility(View.VISIBLE);
		}else
		{
			holder.storeUsed.setVisibility(View.GONE);
		}
		holder.storeNamePhone.setText(address.getShipTo() + " " +address.getCellPhone());
		holder.storeArea.setText(address.getRegionAddress());
		holder.storeDetail.setText(address.getAddress());
		String distance = mContext.getString(R.string.store_distance) + RongJiUtils.getDistance(lat, lng, address.getLatitude(), address.getLongitude(), defaultAdd) + mContext.getString(R.string.store_km);
		if(position == 0)
		{
			holder.storeDistance.setText(Html.fromHtml("<font color='#E77817'>"+ mContext.getString(R.string.store_in_use) +"</font>" + "&nbsp;&nbsp;" + "<font color='#FF0000'>" + distance +"</font>"));
		}else
		{
			holder.storeDistance.setText(Html.fromHtml("<font color='#FF0000'>" + distance +"</font>"));
		}
		
		return view;
	}
	
	private class ViewHolder
	{
		public TextView storeNamePhone;
		public TextView storeArea;
		public TextView storeDetail;
		public TextView storeUsed;
		public TextView storeDistance;
	}
}
